package com.buba.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.service.SpareLedgerService;
import com.buba.service.SpareTypesService;
import common.R;
import entity.sparemanage.SpareLedger;
import entity.sparemanage.SpareTypes;
import entity.sparemanage.dto.selAllSpareTypesByLimitDto;
import entity.sparemanage.vo.selOneSpartTypeVo;
import entity.system.CodeRules;
import feign.systemFeign;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.PageResult;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 备件类型 前端控制器
 * </p>
 *
 * @author beisheng
 * @since 2023-05-18
 */
@RestController
@RequestMapping("/spare-types")
public class SpareTypesController {

    @Autowired
    private SpareTypesService spareTypesService;

    @Autowired
    private SpareLedgerService spareLedgerService;

    @Autowired
    private systemFeign systemFeign;
    /* *
     * @Author: beisheng
     * @Descriprion: 树形查询备件类型
     * @Date: 2023/5/19 15:20
     * @Return: common.R
     * @Param: []
     */

    @GetMapping("/selAllSpareType")
    public R selAllSpareType(){
        List<SpareTypes> spareTypesList = spareTypesService.selAllSpareType();
        return R.success(spareTypesList);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 分页模糊查寻备件类型
     * @Date: 2023/6/1 15:10
     * @Return: common.R
     * @Param: [typesByLimitDto]
     */

    @PostMapping("/selAllSpareTypesByLimit")
    public R selAllSpareTypesByLimit(@RequestBody selAllSpareTypesByLimitDto typesByLimitDto){
        PageResult pageResult = spareTypesService.selAllSpareTypesByLimit(typesByLimitDto);
        return R.success(pageResult);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 新增备件类型
     * @Date: 2023/6/1 17:02
     * @Return: common.R
     * @Param: [spareTypes]
     */

    @PostMapping("/addSpareTypes")
    public R addSpareTypes(@RequestBody SpareTypes spareTypes){
        //1.查询编码生成规则，进行生成编码
        CodeRules bj = systemFeign.selAllRules("BJLX");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(bj.getDateTime());
        String format = simpleDateFormat.format(new Date());
        List<SpareTypes> spareLedgerList = spareTypesService.list(new QueryWrapper<SpareTypes>().eq("delete_flag", 0));
        String code = bj.getPrefix()+format+"0"+(spareLedgerList.size()+1);
        spareTypes.setSpTypecode(code);
        spareTypes.setDeleteFlag(0);
        spareTypes.setCreateTime(new Date());
        spareTypes.setUpdateTime(new Date());
        //2.判断是否是第一级类型
        if (spareTypes.getParentId()!=null){
            spareTypesService.save(spareTypes);
        }else{
            spareTypes.setParentId(0);
            spareTypesService.save(spareTypes);
        }
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据ID查询备件类型信息
     * @Date: 2023/6/1 17:05
     * @Return: common.R
     * @Param: [id]
     */

    @GetMapping("/selOneSpartType/{id}")
    public R selOneSpartType(@PathVariable("id") Integer id){
        SpareTypes spareTypes = spareTypesService.getById(id);
        selOneSpartTypeVo selOneSpartTypeVo = new selOneSpartTypeVo();
        BeanUtils.copyProperties(spareTypes,selOneSpartTypeVo);
        return R.success(selOneSpartTypeVo);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 修改备件类型
     * @Date: 2023/6/1 17:06
     * @Return: common.R
     * @Param: [spareTypes]
     */

    @PostMapping("/updateSpartType")
    public R updateSpartType(@RequestBody SpareTypes spareTypes){
        spareTypes.setUpdateTime(new Date());
        spareTypesService.updateById(spareTypes);
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 批量删除备件类型
     * @Date: 2023/6/1 17:09
     * @Return: common.R
     * @Param: [ids]
     */

    @GetMapping("/deleteBatch/{id}")
    public R deleteBatch(@PathVariable("id") Integer id){
            SpareTypes spareTypes = new SpareTypes();
            spareTypes.setDeleteFlag(0);
            spareTypes.setSpId(id);
        spareTypesService.updateById(spareTypes);
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 启用备件类型
     * @Date: 2023/6/1 19:52
     * @Return: common.R
     * @Param: [id]
     */

    @GetMapping("/qiyong/{id}")
    public R qiyong(@PathVariable("id") Integer id){
        SpareTypes spareTypes = new SpareTypes();
        spareTypes.setSpTypestatus(1);
        spareTypes.setUpdateTime(new Date());
        spareTypes.setSpId(id);
        spareTypesService.updateById(spareTypes);
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 启用备件类型
     * @Date: 2023/6/1 19:52
     * @Return: common.R
     * @Param: [id]
     */

    @GetMapping("/jinyong/{id}")
    public R jinyong(@PathVariable("id") Integer id){
        SpareTypes spareTypes = new SpareTypes();
        spareTypes.setSpTypestatus(2);
        spareTypes.setUpdateTime(new Date());
        spareTypes.setSpId(id);
        spareTypesService.updateById(spareTypes);
        return R.success();
    }


    /* *
     * @Author: beisheng
     * @Descriprion: 查询是否有备件正在使用当前备件类型
     * @Date: 2023/6/1 19:55
     * @Return: common.R
     * @Param: [id]
     */

    @GetMapping("/getShuju/{id}")
    public R getShuju(@PathVariable("id") Integer id){
        List<SpareLedger> list = spareLedgerService.list(new QueryWrapper<SpareLedger>().eq("delete_flag", 0).eq("sp_type_id", id));
        return R.success(list.size());
    }
}

